<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div class="btns">
        <button data-path="/home">首页</button>
        <button data-path="/news">新闻</button>
        <button data-path="/music">音乐</button>
    </div>
    <div id="content"></div>
    <script>
        const con = document.querySelector("#content")
        // 路由表
        const routes = [
            { path: '/home', name: "这是首页" },
            { path: '/news', name: "这是新闻" },
            { path: '/music', name: "这是音乐" }
        ]
        const Rbtns = document.querySelectorAll(".btns button")
        Rbtns.forEach(item => {
            item.onclick = function () {
                location.hash = this.dataset.path
            }
        })
        window.onhashchange = function (e) {
            const newHash = e.newURL.split("#")[1]
            routes.forEach(item => {
                if (item.path === newHash) {
                    con.innerHTML = item.name
                }
            })
        }
    </script>
</body>

</html>